package postgres

import (
	"context"
	"gorm.io/gorm"
	"manager/errors"
	"manager/model"
	"manager/model/entity"
)

var UserSetting = &userSetting{}

type userSetting struct{}

// Create 创建
func (a *userSetting) Create(ctx context.Context, m *entity.UserSetting) (int64, error) {
	err := GetDB(ctx).Create(m).Error
	return m.Id, err
}

// Find 查找详情
func (a *userSetting) Find(ctx context.Context, in *model.UserSettingInfoRequest) (*entity.UserSetting, error) {
	e := &entity.UserSetting{}

	q := GetDB(ctx).Model(&entity.UserSetting{})

	if in.Id > 0 {
		err := q.First(&e, in.Id).Error
		return e, err
	}

	count := 0

	if in.UserId != nil {
		q = q.Where("user_id = ?", in.UserId)
		count++
	}

	if in.Face != nil {
		q = q.Where("face = ?", in.Face)
		count++
	}

	if in.Fingerprint != nil {
		q = q.Where("fingerprint = ?", in.Fingerprint)
		count++
	}

	if in.Vibration != nil {
		q = q.Where("vibration = ?", in.Vibration)
		count++
	}

	if in.CutPower != nil {
		q = q.Where("cut_power = ?", in.CutPower)
		count++
	}

	if in.ChargeMonitor != nil {
		q = q.Where("charge_monitor = ?", in.ChargeMonitor)
		count++
	}

	if in.GuardAlarm != nil {
		q = q.Where("guard_alarm = ?", in.GuardAlarm)
		count++
	}

	if in.FaultAlarm != nil {
		q = q.Where("fault_alarm = ?", in.FaultAlarm)
		count++
	}

	if in.UpdatedAt != nil {
		q = q.Where("updated_at = ?", in.UpdatedAt)
		count++
	}

	if count == 0 {
		return e, errors.New("condition illegal")
	}

	err := q.First(&e).Error
	return e, err
}

// Update 更新
func (a *userSetting) Update(ctx context.Context, id int64, dict map[string]interface{}) error {
	return GetDB(ctx).Model(&entity.UserSetting{}).Where("id = ?", id).Updates(dict).Error
}

// Delete 删除
func (a *userSetting) Delete(ctx context.Context, id int64) error {
	return GetDB(ctx).Delete(&entity.UserSetting{}, id).Error
}

// List 列表查询
func (a *userSetting) List(ctx context.Context, in *model.UserSettingListRequest) (int, []*entity.UserSetting, error) {
	var (
		q            = GetDB(ctx).Model(&entity.UserSetting{})
		err          error
		total        int64
		userSettings []*entity.UserSetting
	)

	if in.UserId != nil {
		q = q.Where("user_id = ?", in.UserId)
	}

	if in.Face != nil {
		q = q.Where("face = ?", in.Face)
	}

	if in.Fingerprint != nil {
		q = q.Where("fingerprint = ?", in.Fingerprint)
	}

	if in.Vibration != nil {
		q = q.Where("vibration = ?", in.Vibration)
	}

	if in.CutPower != nil {
		q = q.Where("cut_power = ?", in.CutPower)
	}

	if in.ChargeMonitor != nil {
		q = q.Where("charge_monitor = ?", in.ChargeMonitor)
	}

	if in.GuardAlarm != nil {
		q = q.Where("guard_alarm = ?", in.GuardAlarm)
	}

	if in.FaultAlarm != nil {
		q = q.Where("fault_alarm = ?", in.FaultAlarm)
	}

	if in.UpdatedAt != nil {
		q = q.Where("updated_at = ?", in.UpdatedAt)
	}

	if err = q.Count(&total).Error; err != nil {
		return 0, nil, err
	}
	if err = q.Order("created_at desc").Limit(in.Size).Offset((in.Index - 1) * in.Size).Find(&userSettings).Error; err != nil {
		return 0, nil, err
	}
	return int(total), userSettings, nil
}

// ExecTransaction db事务执行
func (a *userSetting) ExecTransaction(ctx context.Context, callback func(ctx context.Context) error) error {
	return GetDB(ctx).Transaction(func(tx *gorm.DB) error {
		ctx = context.WithValue(ctx, DBCONTEXTKEY, tx)
		return callback(ctx)
	})
}
